﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EFDAL;
using System.Dynamic;
using System.Threading;

namespace web.Controllers
{
    public class HomeController : Controller
    {
        public HomeController()
        {
        }


        public ZXDDEntities ZXDDEntities { get; set; }

        public HomeController(ZXDDEntities zxddEntities)
        {

            this.ZXDDEntities = zxddEntities;
        }

        public ActionResult Index()
        {
            ViewBag.Count = TempData["count"];
            ViewBag.Item = ZXDDEntities.Employees.FirstOrDefault();
            ViewBag.List = ZXDDEntities.Employees;
            return View();
        }

        public ActionResult About()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Save(FormCollection form)
        {
            var value = form.Count;
            for (int i = 0; i < value / 2; i++)
            {
                int id = Convert.ToInt32(form["id-" + i]);
                Employee e = ZXDDEntities.Employees.First(u => u.id == id);
                var name = form["name-" + i];
                if (e != null & !e.name.Equals(name))
                {
                    e.name = name;
                }
            }

            TempData["count"] = ZXDDEntities.SaveChanges();

            return RedirectToAction("index");

        }
        public ActionResult Test()
        {
            var query = ZXDDEntities.Employees.Select(e => new { txt = e.name });


            dynamic d = new ExpandoObject();
            d.Value = "nihao";
            //ViewBag.Value = d;
            return View(d);
        }
    }
}
